import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/user_info.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/global_easy_loading.dart';
import 'package:flutter_syt/routes.all.dart';
import 'package:flutter_syt/utils/utils_other.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class VenueSubscriptionEntity {
  late String id;
  late String event_name;
  late String unit_name;
  late String status;
  late String status_name;
  late String code;
  late String venue_name;
  late String schedule_name;
  late String discipline_name;
  late String date;
  late String time;
  late bool subscribed;
  String? locationName;
  late int videoType; // 0 无关联 1直播 2点播

  VenueSubscriptionEntity(Map<String, dynamic> json) {
    this.id = "${json["id"]}";
    this.event_name = "${json["event_name"]}";
    this.unit_name = "${json["unit_name"]}";
    this.status = "${json["status"]}";
    this.status_name = "${json["status_name"]}";
    this.code = "${json["code"]}";
    this.venue_name = "${json["venue_name"]}";
    this.date = "${json["date"]}";
    this.time = "${json["time"]}";
    this.discipline_name = "${json["discipline_name"]}";
    this.schedule_name = "${json["schedule_name"]}";
    try {
      subscribed = json["subscribed"] as bool;
    } catch (e) {
      subscribed = false;
    }
    this.locationName = json['location_name'] as String?;
    try {
      videoType = json["game_video_type"] as int;
    } catch (e) {
      videoType = 0;
    }
  }

  Map<String, dynamic> get json {
    var result = Map<String, dynamic>();
    result["id"] = this.id;
    result["event_name"] = this.event_name;
    result["unit_name"] = this.unit_name;
    result["status"] = this.status;
    result["status_name"] = this.status_name;
    result["code"] = this.code;
    result["venue_name"] = this.venue_name;
    result["date"] = this.date;
    result["time"] = this.time;
    result['location_name'] = this.locationName;
    result["schedule_name"] = this.schedule_name;
    result["subscribed"] = this.subscribed;
    result["game_video_type"] = this.videoType;
    return result;
  }
}

// import 'package:flutter/material.dart';

class VenueSubscriptionItem extends StatefulWidget {
  final VenueSubscriptionEntity entity;

  VenueSubscriptionItem(this.entity);
  @override
  VenueSubscriptionItemState createState() => VenueSubscriptionItemState();
}

class VenueSubscriptionItemState extends State<VenueSubscriptionItem> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(12)),
      ),
      child: Column(
        children: [
          SizedBox(
            height: 12,
          ),
          Row(
            children: [
              Text(
                widget.entity.date,
                style: TextStyle(
                  color: Colors.black.withOpacity(0.6),
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                ),
              ),
              Padding(padding: EdgeInsets.only(left: 7)),
              Text(
                widget.entity.time,
                style: TextStyle(
                  color: Colors.black,
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                ),
              ),
              Spacer(),
              Text(
                widget.entity.status_name,
                style: TextStyle(
                  color: Colors.black.withOpacity(0.4),
                  fontSize: 11,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
          Padding(padding: EdgeInsets.only(top: 4)),
          Row(
            children: [
              Flexible(
                  child: RichText(
                textAlign: TextAlign.left,
                text: TextSpan(
                  children: [
                    WidgetSpan(
                        child: widget.entity.videoType != 0
                            ? Padding(
                                padding: EdgeInsets.only(right: 4, bottom: 2),
                                child: widget.entity.videoType == 1
                                    ? Image.asset(
                                        "assets/icons/game_event_live.gif",
                                        width: 14,
                                        height: 14)
                                    : Image.asset(
                                        "assets/icons/game_event_video_ic.png",
                                        width: 16,
                                        height: 14),
                              )
                            : Text('')),
                    TextSpan(
                      text:
                          "${widget.entity.discipline_name} ${widget.entity.schedule_name}",
                      style: (TextStyle(
                        fontWeight: FontWeight.w500,
                        fontSize: 16,
                        color: Colors.black.withOpacity(0.8),
                      )),
                    ),
                  ],
                ),
              )),
              // Flexible(
              //   child: Text(
              //     widget.entity.schedule_name,
              //     style: TextStyle(
              //         color: Colors.black.withOpacity(0.8),
              //         fontSize: 16,
              //         fontWeight: FontWeight.w500),
              //   ),
              // ),
            ],
          ),
          Padding(padding: EdgeInsets.only(top: 10)),
          Row(
            children: [
              Image.asset(
                'assets/icons/schedule_location.png',
                width: 10,
              ),
              SizedBox(
                width: 4,
              ),
              Expanded(
                child: Text(
                  // widget.entity.locationName
                  "${widget.entity.venue_name} ${widget.entity.locationName}",
                  style: TextStyle(
                    color: Colors.black.withOpacity(0.4),
                    // fontWeight: FontWeight.w300,
                    fontSize: 12,
                  ),
                  maxLines: 2,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              // Spacer(),
              SizedBox(
                width: 12,
              ),
              Visibility(
                  visible: widget.entity.status != 'FINISHED',
                  child: GestureDetector(
                    onTap: () async {
                      if (!UserInfo.me.isLogin) {
                        RouteManager.push(
                          context,
                          RoutMapping.loginByPwd(),
                        );
                        return;
                      }
                      if (widget.entity.subscribed) {
                        try {
                          await API.shared.game
                              .cancelMatchSubscribe(widget.entity.id);
                          widget.entity.subscribed = false;
                          customShowToast(S.of(context).unsubscribe_succeeded);
                          setState(() {});
                        } catch (e) {
                          await UtilsOther.showCustomShowError(e);
                          // if (e is DioError) {
                          //   customShowToast(e.message);
                          // } else {
                          //   customShowToast(S.of(context).network_error);
                          // }
                        }
                      } else {
                        try {
                          await API.shared.game
                              .subscribeMatch(widget.entity.id);
                          widget.entity.subscribed = true;
                          customShowToast(S.of(context).subscribe_succeeded);
                          setState(() {});
                        } catch (e) {
                          await UtilsOther.showCustomShowError(e);
                          // if (e is DioError) {
                          //   customShowToast(e.message);
                          // } else {
                          //   customShowToast(S.of(context).network_error);
                          // }
                        }
                      }
                    },
                    child: Container(
                      height: 28,
                      padding: EdgeInsets.symmetric(horizontal: 16),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(4)),
                        color: widget.entity.subscribed
                            ? Color(0xffF7D749).withOpacity(0.3)
                            : Color(0xffF7D749),
                      ),
                      child: Text(
                        widget.entity.subscribed
                            ? S().Subscribed
                            : S().Subscribe,
                        maxLines: 1,
                        style: TextStyle(
                          color: widget.entity.subscribed
                              ? Colors.black.withOpacity(0.6)
                              : Colors.black,
                          // fontWeight: FontWeight.w500,
                          fontSize: 12,
                        ),
                      ),
                    ),
                  ))
            ],
          ),
          Container(
            margin: EdgeInsets.only(top: 12),
            height: 0.5,
            color: Colors.black.withOpacity(0.1),
          )
        ],
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(VenueSubscriptionItem oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}

// class VenueSubscriptionItem extends StatelessWidget {
//   final VenueSubscriptionEntity widget.entity;

//   VenueSubscriptionItem(this.widget.entity);

//   @override
//   Widget build(BuildContext context) {
// print("data -> ${widget.entity.json}");
//   return Container(
//     padding: EdgeInsets.symmetric(vertical: 14, horizontal: 15),
//     decoration: BoxDecoration(
//       color: Colors.white,
//       borderRadius: BorderRadius.all(Radius.circular(12)),
//     ),
//     child: Column(
//       children: [
//         Row(
//           children: [
//             Text(
//               widget.entity.date,
//               style: TextStyle(
//                 color: Colors.black.withOpacity(0.6),
//                 fontSize: 14,
//                 fontWeight: FontWeight.w400,
//               ),
//             ),
//             Padding(padding: EdgeInsets.only(left: 7)),
//             Text(
//               widget.entity.time,
//               style: TextStyle(
//                 color: Colors.black,
//                 fontSize: 14,
//                 fontWeight: FontWeight.w400,
//               ),
//             ),
//             Spacer(),
//             Text(
//               widget.entity.status_name,
//               style: TextStyle(
//                 color: Colors.black.withOpacity(0.4),
//                 fontSize: 11,
//                 fontWeight: FontWeight.w400,
//               ),
//             ),
//           ],
//         ),
//         Padding(padding: EdgeInsets.only(top: 4)),
//         Row(
//           children: [
//             Flexible(
//                 child: RichText(
//               textAlign: TextAlign.left,
//               text: TextSpan(
//                 children: [
//                   WidgetSpan(
//                       child: widget.entity.videoType != 0
//                           ? Padding(
//                               padding: EdgeInsets.only(right: 4, bottom: 2),
//                               child: widget.entity.videoType == 1
//                                   ? Image.asset(
//                                       "assets/icons/game_event_live.gif",
//                                       width: 14,
//                                       height: 14)
//                                   : Image.asset(
//                                       "assets/icons/game_event_video_ic.png",
//                                       width: 16,
//                                       height: 14),
//                             )
//                           : Text('')),
//                   TextSpan(
//                     text:
//                         "${widget.entity.discipline_name} ${widget.entity.schedule_name}",
//                     style: (TextStyle(
//                       fontWeight: FontWeight.w500,
//                       fontSize: 16,
//                       color: Colors.black.withOpacity(0.8),
//                     )),
//                   ),
//                 ],
//               ),
//             )),
//             // Flexible(
//             //   child: Text(
//             //     widget.entity.schedule_name,
//             //     style: TextStyle(
//             //         color: Colors.black.withOpacity(0.8),
//             //         fontSize: 16,
//             //         fontWeight: FontWeight.w500),
//             //   ),
//             // ),
//           ],
//         ),
//         Padding(padding: EdgeInsets.only(top: 10)),
//         Row(
//           children: [
//             Image.asset(
//               'assets/icons/schedule_location.png',
//               width: 10,
//             ),
//             Expanded(
//               child: Text(
//                 // widget.entity.locationName
//                 "${widget.entity.venue_name} ${widget.entity.locationName}",
//                 style: TextStyle(
//                   color: Colors.black.withOpacity(0.4),
//                   fontWeight: FontWeight.w300,
//                   fontSize: 12,
//                 ),
//                 maxLines: 2,
//                 overflow: TextOverflow.ellipsis,
//               ),
//             ),
//             // Spacer(),
//             SizedBox(
//               width: 12,
//             ),
//             GestureDetector(
//               onTap: () async {
//                     if (!UserInfo.me.isLogin) {
//                       RouteManager.push(
//                         context,
//                         RoutMapping.loginByPwd(),
//                       );
//                       return;
//                     }
//                     if (widget.entity.subscribed) {
//                       try {
//                         await API.shared.game
//                             .cancelMatchSubscribe(widget.entity.id);
//                         widget.entity.subscribed = false;
//                         customShowToast(S.of(context).unsubscribe_succeeded);
//                         // setState(() {});

//                       } catch (e) {
//                         if (e is DioError) {
//                           customShowToast(e.message);
//                         } else {
//                           customShowToast(S.of(context).network_error);
//                         }
//                       }
//                     } else {
//                       try {
//                         await API.shared.game
//                             .subscribeMatch(widget.entity.id);
//                         widget.entity.subscribed = true;
//                         customShowToast(S.of(context).subscribe_succeeded);
//                         setState(() {});
//                       } catch (e) {
//                         if (e is DioError) {
//                           customShowToast(e.message);
//                         } else {
//                           customShowToast(S.of(context).network_error);
//                         }
//                       }
//                     }
//                   },
//               child: Container(
//                 height: 28,
//                 padding: EdgeInsets.symmetric(horizontal: 16),
//                 alignment: Alignment.center,
//                 decoration: BoxDecoration(
//                   borderRadius: BorderRadius.all(Radius.circular(4)),
//                   color: widget.entity.subscribed
//                       ? Color(0xffF7D749).withOpacity(0.3)
//                       : Color(0xffF7D749),
//                 ),
//                 child: Text(
//                   widget.entity.subscribed ? S().Subscribed : S().Subscribe,
//                   maxLines: 1,
//                   style: TextStyle(
//                     color: widget.entity.subscribed
//                         ? Colors.black.withOpacity(0.6)
//                         : Colors.black,
//                     // fontWeight: FontWeight.w500,
//                     fontSize: 12,
//                   ),
//                 ),
//               ),
//             )
//           ],
//         ),
//       ],
//     ),
//   );
// }
// }
